package com.bocloud.cmp.server.controller.resource;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.interfaces.resource.CloudServerService;
import com.bocloud.cmp.model.CloudServerBean;
import com.bocloud.common.model.BsmResult;
import com.bocloud.common.model.Param;
import com.bocloud.common.model.RequestUser;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.JSONTools;
import com.bocloud.common.utils.ResultTools;

/**
 * 云主机相关操作Controller入口
 * 
 * @author dongkai
 *
 */
@RestController
@RequestMapping("/vm")
public class CloudServerController {

    @Autowired
    private CloudServerService cloudServerService;

    /**
     * 获取云主机列表
     * 
     * @param page
     *            当前页码
     * @param rows
     *            页面数据大小
     * @param params
     *            查询参数，例如：[{"param":{"name":"aaa","password":"1245"},"sign":
     *            "EQ|UEQ"},{"param":{"name":"aaa","password":"1245"},"sign":
     *            "EQ|UEQ"}]
     * @param sorter
     *            排序参数，例如：{"name":0|1,"password":0|1},0表示增序，1表示降序
     * @param simple
     *            简单查询标记，只有true和false,为false时返回云主机的详细信息，为true时只返回id和name值。
     * @return 数据结果集
     */
    @RequestMapping(value = "/list", method = { RequestMethod.POST })
    public BsmResult list(@RequestParam(value = Common.PAGE, required = false) Integer page,
            @RequestParam(value = Common.ROWS, required = false) Integer rows,
            @RequestParam(value = Common.PARAMS, required = false) String params,
            @RequestParam(value = Common.SORTER, required = false) String sorter,
            @RequestParam(value = Common.SIMPLE, required = false) Boolean simple,
            @Value(Common.REQ_USER) RequestUser user) {
        @SuppressWarnings("unchecked")
        Map<String, String> sorterMap = JSONObject.parseObject(sorter, HashMap.class);
        return cloudServerService.list(page, rows, JSONObject.parseArray(params, Param.class), sorterMap, simple);

    }

    /**
     * 添加云主机
     * 
     * @param params
     *            云主机属性信息的json字符串，在数据转换时会发生异常，对外抛出400异常【Bad Request】
     * @return 添加结果
     */
    @RequestMapping(value = "/create", method = { RequestMethod.POST })
    public BsmResult create(@RequestParam(value = Common.PARAMS, required = true) String params,
            @Value(Common.REQ_USER) RequestUser user) {
        return cloudServerService.create(params, user.getId());
    }

    /**
     * 克隆云主机
     * 
     * @param params
     *            云主机属性信息的json字符串，在数据转换时会发生异常，对外抛出400异常【Bad Request】
     * @return 添加结果
     */
    @RequestMapping(value = "/clone", method = { RequestMethod.POST })
    public BsmResult clone(@RequestParam(value = Common.PARAMS, required = true) String params,
            @Value(Common.REQ_USER) RequestUser user) {
        return cloudServerService.clone(params, user.getId());
    }

    /**
     * 迁移
     * 
     * @param params
     * @param user
     * @return
     */
    @RequestMapping(value = "/migrate", method = { RequestMethod.POST })
    public BsmResult migrate(@RequestParam(value = Common.PARAMS, required = true) String params,
            @Value(Common.REQ_USER) RequestUser user) {
        return cloudServerService.migrate(params, user.getId());
    }

    /**
     * 迁移(openstack)
     * 
     * @param id
     * @param user
     * @return
     */
    @RequestMapping(value = "/OSMigrate", method = { RequestMethod.POST })
    public BsmResult OSMigrate(@RequestParam(value = Common.PARAMS, required = true) String params,
            @Value(Common.REQ_USER) RequestUser user) {
        return cloudServerService.OSMigrate(params, user.getId());
    }

    /**
     * 创建快照
     * 
     * @param params
     * @param user
     * @return
     */
    @RequestMapping(value = "/snapshot", method = { RequestMethod.POST })
    public BsmResult snapshot(@RequestParam(value = Common.PARAMS, required = true) String params,
            @Value(Common.REQ_USER) RequestUser user) {
        return cloudServerService.snapshot(params, user.getId());
    }

    /**
     * 修改云主机
     * 
     * @param params
     *            云主机属性信息的json字符串，在数据转换时会发生异常，对外抛出400异常【Bad Request】
     * @param id
     *            需要修改的云主机ID
     */
    @RequestMapping(value = "/{id}/modify", method = { RequestMethod.POST })
    public BsmResult modify(@RequestParam(value = Common.PARAMS, required = true) String params,
            @PathVariable(value = Common.ID) Long id, @Value(Common.REQ_USER) RequestUser user) {
        JSONObject object = JSONTools.isJSONObj(params);
        if (null != object) {
            CloudServerBean bean = JSONObject.parseObject(object.toJSONString(), CloudServerBean.class);
            return cloudServerService.modify(bean, user.getId());
        } else {
            return ResultTools.formatErrResult();
        }
    }

    /**
     * 删除云主机
     * 
     * @param id
     *            云主机ID
     * @return 操作结果
     */
    @RequestMapping(value = "/{id}/remove", method = { RequestMethod.DELETE })
    public BsmResult remove(@PathVariable(value = Common.ID) Long id, @Value(Common.REQ_USER) RequestUser user) {
        return cloudServerService.remove(id, user.getId());
    }

    /**
     * 查看云主机详细信息
     * 
     * @param id
     *            云主机ID
     * @param user.getId()
     *            操作者ID
     * @return 操作结果
     */
    @RequestMapping(value = "/{id}/detail", method = { RequestMethod.GET })
    public BsmResult detail(@PathVariable(value = Common.ID) Long id, @Value(Common.REQ_USER) RequestUser user) {
        return cloudServerService.detail(id);
    }

    /**
     * 重启一台云主机
     * 
     * @param id
     *            云主机ID
     * @param user.getId()
     *            操作者ID
     * @return 操作结果
     */
    @RequestMapping(value = "/{id}/restart", method = { RequestMethod.POST })
    public BsmResult restart(@PathVariable(value = Common.ID) Long id, @Value(Common.REQ_USER) RequestUser user) {
        return cloudServerService.restart(id, user.getId());
    }

    /**
     * 暂停
     * 
     * @param id
     * @param user
     * @return
     */
    @RequestMapping(value = "/{id}/pause", method = { RequestMethod.POST })
    public BsmResult pause(@PathVariable(value = Common.ID) Long id, @Value(Common.REQ_USER) RequestUser user) {
        return cloudServerService.pause(id, user.getId());
    }

    /**
     * 恢复
     * 
     * @param id
     * @param user
     * @return
     */
    @RequestMapping(value = "/{id}/recovery", method = { RequestMethod.POST })
    public BsmResult recovery(@PathVariable(value = Common.ID) Long id, @Value(Common.REQ_USER) RequestUser user) {
        return cloudServerService.recovery(id, user.getId());
    }

    /**
     * 挂起
     * 
     * @param id
     * @param user
     * @return
     */
    @RequestMapping(value = "/{id}/suspend", method = { RequestMethod.POST })
    public BsmResult suspend(@PathVariable(value = Common.ID) Long id, @Value(Common.REQ_USER) RequestUser user) {
        return cloudServerService.suspend(id, user.getId());
    }

    /**
     * 激活
     * 
     * @param id
     * @param user
     * @return
     */
    @RequestMapping(value = "/{id}/active", method = { RequestMethod.POST })
    public BsmResult active(@PathVariable(value = Common.ID) Long id, @Value(Common.REQ_USER) RequestUser user) {
        return cloudServerService.active(id, user.getId());
    }

    /**
     * 启动
     * 
     * @param id
     * @param user
     * @return
     */
    @RequestMapping(value = "/{id}/start", method = { RequestMethod.POST })
    public BsmResult start(@PathVariable(value = Common.ID) Long id, @Value(Common.REQ_USER) RequestUser user) {
        return cloudServerService.start(id, user.getId());
    }

    /**
     * 虚拟机批量重启
     * 
     * @param params
     * @param user
     * @return
     */
    @RequestMapping(value = "/batchReboot", method = { RequestMethod.POST })
    public BsmResult batchReboot(@RequestParam(value = Common.PARAMS, required = true) String params,
            @Value(Common.REQ_USER) RequestUser user) {
        return cloudServerService.batchReboot(params, user.getId());
    }

    /**
     * 虚拟机批量启动
     * 
     * @param params
     * @param user
     * @return
     */
    @RequestMapping(value = "/batchStart", method = { RequestMethod.POST })
    public BsmResult batchStart(@RequestParam(value = Common.PARAMS, required = true) String params,
            @Value(Common.REQ_USER) RequestUser user) {
        return cloudServerService.batchStart(params, user.getId());
    }

    /**
     * 虚拟机批量停止
     * 
     * @param params
     * @param user
     * @return
     */
    @RequestMapping(value = "/batchStop", method = { RequestMethod.POST })
    public BsmResult batchStop(@RequestParam(value = Common.PARAMS, required = true) String params,
            @Value(Common.REQ_USER) RequestUser user) {
        return cloudServerService.batchStop(params, user.getId());
    }

    /**
     * 虚拟机批量删除
     * 
     * @param params
     * @param user
     * @return
     */
    @RequestMapping(value = "/batchRemove", method = { RequestMethod.POST })
    public BsmResult batchRemove(@RequestParam(value = Common.PARAMS, required = true) String params,
            @Value(Common.REQ_USER) RequestUser user) {
        return cloudServerService.batchRemove(params, user.getId());
    }

    /**
     * 关机
     * 
     * @param id
     * @param user
     * @return
     */
    @RequestMapping(value = "/{id}/stop", method = { RequestMethod.POST })
    public BsmResult stop(@PathVariable(value = Common.ID) Long id, @Value(Common.REQ_USER) RequestUser user) {
        return cloudServerService.stop(id, user.getId());
    }

    /**
     * 备份一台云主机
     * 
     * @param id
     *            云主机ID
     * @param user.getId()
     *            操作者ID
     * @return 操作结果
     */
    @RequestMapping(value = "/{id}/backup", method = { RequestMethod.POST })
    public BsmResult backup(@PathVariable(value = Common.ID) Long id, @Value(Common.REQ_USER) RequestUser user) {
        return cloudServerService.backup(id, user.getId());
    }

    /**
     * 获取监控数据接口
     * 
     * @param params
     * @param user
     * @return
     */
    @RequestMapping(value = "/performance", method = { RequestMethod.GET })
    public BsmResult performance(@RequestParam(value = Common.PARAMS, required = false) String params,
            @Value(Common.REQ_USER) RequestUser user) {
        JSONObject object = JSONTools.isJSONObj(params);
        if (null != object) {
            return cloudServerService.getPerformance(params);
        } else {
            return ResultTools.formatErrResult();
        }
    }

    /**
     * vnc console接口
     * 
     * @param params
     * @param user
     * @return
     */
    @RequestMapping(value = "/vncconsole", method = { RequestMethod.GET })
    public BsmResult vncconsole(@RequestParam(value = Common.PARAMS, required = false) String params,
            @Value(Common.REQ_USER) RequestUser user) {
        JSONObject object = JSONTools.isJSONObj(params);
        if (null != object) {
            return cloudServerService.vncconsole(params);
        } else {
            return ResultTools.formatErrResult();
        }
    }

    /**
     * 网络列表
     * 
     * @param id
     *            任务ID
     * @return 操作结果
     */
    @RequestMapping(value = "/{id}/networks", method = { RequestMethod.GET })
    public BsmResult networks(@PathVariable(value = Common.ID) Long id) {
        return cloudServerService.networks(id);
    }

    /**
     * unpause
     * 
     * @param id
     * @param user
     * @return
     */
    @RequestMapping(value = "/{id}/unpause", method = { RequestMethod.POST })
    public BsmResult unpause(@PathVariable(value = Common.ID) Long id, @Value(Common.REQ_USER) RequestUser user) {
        return cloudServerService.unpause(id, user.getId());
    }

}
